package com.kystar.ledcontrol.recycleview.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.kystar.ledcontrol.recycleview.R;
import com.kystar.ledcontrol.recycleview.adapter.BaseQuickAdapter;
import com.kystar.ledcontrol.recycleview.adapter.OnItemChildClickListener;
import com.kystar.ledcontrol.recycleview.adapter.QuickAdapter;
import com.kystar.ledcontrol.recycleview.adapter.VHolder;
import com.kystar.ledcontrol.recycleview.util.FileManager;
import com.kystar.ledcontrol.recycleview.util.ImgFolderBean;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    public static final String PARAM = "imageDir";
    List<ImgFolderBean> mImgFolderBeanList;
    RecyclerView mRecyclerView;
    QuickAdapter<ImgFolderBean> mAdapter;
    RxPermissions mRxPermissions;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FileManager fileManager = FileManager.getInstance(this);

        mRecyclerView = findViewById(R.id.recycleView);
        mRxPermissions = new RxPermissions(MainActivity.this);
        mRxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(granted -> {
                    if (granted) {
                        mImgFolderBeanList = FileManager.getImageFolders();
                        if(mAdapter != null) {
                            mAdapter.setNewData(mImgFolderBeanList);
                        }
                    } else {

                    }
                });
        mAdapter = new QuickAdapter<ImgFolderBean>(R.layout.item_photo_dir,mImgFolderBeanList) {
            @Override
            protected void convert(VHolder holder, ImgFolderBean imgFolderBean) {
                holder.addOnClickListener(R.id.image);
                holder.setText(R.id.name,imgFolderBean.getName());
                Glide.with(MainActivity.this).load(imgFolderBean.getFistImgPath()).into((ImageView) holder.getView(R.id.image));
            }
        };
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,4));
        mRecyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
           @Override
           public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
               ImgFolderBean imgFolderBean = (ImgFolderBean) adapter.getData().get(position);
               Intent intent = new Intent(MainActivity.this,ImagePickerActivity.class);
               intent.putExtra(PARAM,imgFolderBean.getDir());
               startActivity(intent);
           }
       });
    }

}
